
#include "GameCommon.h"
#include "Memory.h"
// Project headers
#include "Log.h"
#include "System/Display.h"
#include "System/TouchInput.h"
#include "Game.h"
#include "sdl/SDL_timer.h"

// OpenGL Headers
#ifdef WIN32
	#include <windows.h>
	#include <gl\gl.h>
	#include <gl\glu.h>
#elif defined __MACOS__
	#include <OpenGLES/ES1/gl.h>
	#include <OpenGLES/EAGL.h>
	#include "iphone/EAGLView.h"
#else
	#include <CoreSurface/CoreSurface.h>
	#include <OpenGLES/gl.h>
	#include <OpenGLES/egl.h>
	#include "iphone/GamePlatformIP.h"
#endif

float _cursorXPos = 0.0f;
float _cursorYPos = 0.0f;
float _cursorDragXPos = 0.0f;
float _cursorDragYPos = 0.0f;

unsigned int gTick = 0;
char _AppPath[100];

extern mouseState state;
extern int originalPosX;
extern int originalPosY;

float accelerationX = 0.0;
float accelerationY = 0.0;
float accelerationZ = 0.0;

static bool stc_isKeyboardVisible = false;

bool nCGame::_movieFinished = true;
static char stc_keyInput[50];

bool nCGame::_gamePaused = false;
bool nCGame::_gameRestart = false;
//bool nCGame::_gameNeedSave = false;


#ifdef __MACOS__
extern UIView *g_View;
#endif

char nCGame::stc_ResourcesPath[TEXT_FILENAME_SIZE];
char nCGame::stc_SavePath[TEXT_FILENAME_SIZE];

//#ifdef WIN32
char* nCGame::getInputKeyboard(int *len)
{
	//Function declared on BrainChallenge.mm to get the Input by UIKeyboard
#ifdef __MACOS__
	[g_View getTextInput: stc_keyInput];
#endif
	*len = strlen(stc_keyInput);
	return stc_keyInput;
}

bool nCGame::isInputDone()
{
#ifdef __MACOS__
	return [g_View isInputDone];
#else
	return false;
#endif
}

void nCGame::setTextKeyboard(char *text)
{
	//Function declared on BrainChallenge.mm to set the current text on the UIKeyboard
	strcpy(stc_keyInput, text);
}
//#endif

void nCGame::setKeyboardInputVisible(bool v)
{
	stc_isKeyboardVisible = v;

}


bool nCGame::getKeyboardInputVisible()
{
	return stc_isKeyboardVisible;
	//return false;
}

//#if defined __MACOS__
//#else
void nCGame::GetFileName (const char* filename, bool bWrite, char*out)
{
	#if defined WIN32 || defined __MACOS__
		sprintf(out, "%s", filename);
	#else
		sprintf(out, "%s/%s", _AppPath, filename);
	#endif
}
//#endif

// -------------------------------------------------------------
void nCGame::gamePreStart()
{
	GFX_Initialize();
}

// -------------------------------------------------------------
void nCGame::gameStart()
{
	gp_GameApp = GL_NEW CGame();
	gp_GameApp->Initialize();
}

// -------------------------------------------------------------
void nCGame::gamePaint()
{
	gp_GameApp->Paint();
}

//#ifdef WIN32
	unsigned long LastTime;
//#endif

// -------------------------------------------------------------
bool nCGame::gameUpdate()
{
	gTick++;

	// RUIZD: Hardcode the number of microseconds that pass. Change it later to the real number
	// of microseconds (check if the iPhone SDK cames with a function that returns that)
//#ifdef WIN32
	unsigned long t = S_GetTime();
	eIPLUpdateStatus status;

	
	
	if (LastTime == 0)
	{
		status = gp_GameApp->Update(1000); //t - LastTime);
	}
	else
	{
		if (t - LastTime < 1000.0f/MAX_FPS)
		{
			SDL_Delay((1000.0f/MAX_FPS + 0.5f)  - (t - LastTime));
		}
		t = S_GetTime();
		status = gp_GameApp->Update(t - LastTime); //t - LastTime);
	}

	LastTime = t;
/*
#else
	eIPLUpdateStatus status = gp_GameApp->Update(50);
#endif
*/
	

	return status == IPL_US_APP_ACTIVE;
}

// -------------------------------------------------------------
void nCGame::gameStop()
{
	
}

void nCGame::gameRelease()
{
	
}

// -------------------------------------------------------------
void nCGame::gameMouseDown(float x, float y)
{
	_cursorXPos = x;
	_cursorYPos = y;
	
	state = STATE_DOWN;
	originalPosX = (int)x;
	originalPosY = (int)y;
}

// -------------------------------------------------------------
void nCGame::gameMouseUp(float x, float y)
{
	_cursorDragXPos = 0.0f;
	_cursorDragYPos	= 0.0f;

	state = STATE_UP;
	originalPosX = (int)x;
	originalPosY = (int)y;
}

// -------------------------------------------------------------
void nCGame::gameMouseDrag(float x, float y)
{
	_cursorDragXPos = x;
	_cursorDragYPos = y;
	
	state = STATE_DRAGGED;
	originalPosX = (int)x;
	originalPosY = (int)y;
}

void nCGame::gameAccelerated(float x, float y, float z)
{
	accelerationX = x;
	accelerationY = y;
	accelerationZ = z;
}

#ifndef __MACOS__
char *nCGame::GetLanguage()
{
	/// @note: you can hardcode this with any of:
	/// "en", "es", etc
	return "ok";
}

int nCGame::GetOrientation()
{
	return 0;
}
#endif //__MACOS__
